package com.peng.stack_queue.tests;

import java.util.Stack;

/**
 * 用栈实现队列的功能
 */
public class StackWrapToQueue {

    Stack<Integer> pushStack;
    Stack<Integer> popStack;

    public StackWrapToQueue() {
        pushStack = new Stack<>();
        popStack = new Stack<>();
    }

    public static void main(String[] args) {
        StackWrapToQueue stackWrapToQueue = new StackWrapToQueue();
        stackWrapToQueue.push(1);
        stackWrapToQueue.push(2);
        stackWrapToQueue.push(3);
        stackWrapToQueue.push(4);
        stackWrapToQueue.push(5);
        System.out.println(stackWrapToQueue.pop());
        System.out.println(stackWrapToQueue.pop());
        System.out.println(stackWrapToQueue.pop());
        System.out.println(stackWrapToQueue.pop());
        System.out.println(stackWrapToQueue.pop());
        stackWrapToQueue.push(6);
        stackWrapToQueue.push(7);
        stackWrapToQueue.push(8);
        System.out.println(stackWrapToQueue.pop());
        System.out.println(stackWrapToQueue.pop());
        System.out.println(stackWrapToQueue.pop());
    }

    public void push(int val) {
        pushStack.push(val);
        pushToPop();
    }

    private void pushToPop() {
        if (!popStack.isEmpty()) {
            return;
        }

        while (!pushStack.isEmpty()) {
            popStack.push(pushStack.pop());
        }

    }

    public Integer pop() {
        if (pushStack.isEmpty() && popStack.isEmpty()) {
            return null;
        }
        pushToPop();
        return popStack.pop();
    }

    public Integer peek() {
        if (pushStack.isEmpty() && popStack.isEmpty()) {
            return null;
        }
        pushToPop();
        return popStack.peek();
    }

}
